import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import(/* webpackChunkName:"login"*/ '@/views/login/index'),
    hidden: true
  },

  {
    path: '/import',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      component: () => import('@/views/employees/import.vue')
    }]
  },

  {
    path: '/detail/:id',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      component: () => import('@/views/employees/detail.vue')
    }]
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  {
    path: '/404',
    component: () => import(/* webpackChunkName:"login"*/ '@/views/404'),
    hidden: true
  }

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }

]

import departmentsRouter from '@/router/modules/departments'
import employeesRouter from '@/router/modules/employees'
import settingsRouter from '@/router/modules/settings'
import salarysRouter from '@/router/modules/salarys'
import social_securitysRouter from '@/router/modules/social_securitys'
import attendancesRouter from '@/router/modules/attendances'
import approvalsRouter from '@/router/modules/approvals'
import permissionsRouter from '@/router/modules/permissions'

// 动态路由
export const asyncRoutes = [
  departmentsRouter,
  settingsRouter,
  employeesRouter,
  permissionsRouter,
  social_securitysRouter,
  approvalsRouter,
  attendancesRouter,
  salarysRouter
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
